<?php

class mgroup_management extends CI_Model {

        public function __construct()
        {
            parent::__construct();
            $this->load->library('serversidedatatables');
        }
    
        ########################################################################
        ####### DATA GROUP MANAG ##############
        ########################################################################
        function save()
        {
			// $dblokal  = $this->load->database("default", TRUE);
			// $name = $this->input->post("name");
			// $parent = $this->input->post("parent");
			// $stock = $this->input->post("stock");
		
			// $data = array("user_group_name"=>$name,
			// 			  "parent_id"=>$parent,
			// 			  "having_stock"=>$stock,
			// 				"created_by"=>$this->session->userdata('username'),
			// 				"created_on"=>date("Y-m-d H:i:s"));
            
			// if($edit<>""){ 
			// 	$dblokal->where(array("user_group_id"=>$edit));
			// 	$dblokal->update("t_mtr_user_group",$data);
			// }else{
			// 	$dblokal->insert("t_mtr_user_group",$data);
			// }
            $creator = $this->session->userdata('username');
        
            $this->session->set_userdata('group_name', $this->input->post('group_name'));
            $having_stock=0;
            $is_use_top=0;
            $istatus=0;

            if(($this->input->post('istatus') == 'off') || ($this->input->post('istatus') == false))
            {
                $istatus = 0;
            }
            else
            {
                $istatus = 1;
            }

            if(($this->input->post('having_stock') == 'off') || ($this->input->post('having_stock') == false))
            {
                $having_stock = 0;
            }
            else
            {
                $having_stock = 1;
            }

             if(($this->input->post('is_use_top') == 'off') || ($this->input->post('is_use_top') == false))
            {
                $is_use_top = 0;
            }
            else
            {
                $is_use_top = 1;
            }


            $data = array(
                    'user_group_name' => $this->input->post('user_group_name'),
                    'parent_id' => $this->input->post('parent_id'),
                    'user_group_caption' => $this->input->post('caption'),
                    'territory_type_id' => $this->input->post('territory_type_id'),
                    'istatus' => $istatus,
                    'having_stock' => $having_stock,
                    'is_use_top' => $is_use_top,
                    'created_by' => $creator
                );
            // var_dump($data); die();
            $query = $this->db->insert('t_mtr_user_group', $data);
            
            if(isset($query))
            {
                return true;
            }
        }
        
        function getGroup(){
                $dblokal = $this->load->database("default", TRUE);
		
    //             $dblokal->select('*');
				// $dblokal->where(array("istatus"=>1));
    //             $dblokal->from("t_mtr_user_group");
    //             $query = $dblokal->get();
                
    //             if( $query->num_rows() > 0 ) {
    //                     return $query->result();
    //             } else {
    //                     return array();
    //             }
            $sIndexColumn = "alias_id";
            $status=""; 
            $defaultOrder = "0"; 
            $userlogin = $this->session->userdata('username');

            $aColumns = array("id_grup", "nama_grup", "caption", "user_group_name", "teritori", "status"); 
            $str1 = array(" , ", ", ' '");
            $str2 = array(" ", " ");
            $inputQuery = "select ".str_replace($str1, $str2, implode(", ", $aColumns))." from v_ssp_group_management"; 

            return $this->serversidedatatables->executeServerSideProcess($defaultOrder, $status, $userlogin, $inputQuery, $aColumns, $sIndexColumn); 
           
        }

        
        function get($id = "")
        {    
                $dblokal  = $this->load->database("default", TRUE);
                if($id<>""){
                        $dblokal->where(array("user_group_id"=>$id));
                }
				$dblokal->where(array("istatus"=>1));
                $query = $dblokal->get("t_mtr_user_group");
                if( $query->num_rows() > 0 ) {
                        return $query->result();
                } else {
                        return array();
                }
	}
        
        
        #########################
        #########################
        
        public function getById( $id ) {
                #$id = intval( $id );
                
                $query = $this->db->where( 'id', $id )->limit( 1 )->get( 't_mtr_user_group' );
                
                if( $query->num_rows() > 0 ) {
                    return $query->row();
                } else {
                    return array();
                }
        }
        
        public function getAll() {
                //get all records from users table
                $query = $this->db->get( 't_mtr_user_group' );
                
                if( $query->num_rows() > 0 ) {
                    return $query->result();
                } else {
                    return array();
                }
        } //end getAll
        
        public function update($user_group_id, $user_group_name, $caption, $territory_type_id, $parent_id, $istatus, $having_stock, $is_use_top) {
               // $this->session->set_userdata('alias_name_after', $alias_name);
               // $this->session->set_userdata('user_group_id_after', $user_group_id);

                if(($this->input->post('istatus') == 'off') || ($this->input->post('istatus') == false))
                {
                    $istatus = 0;
                }
                else
                {
                    $istatus = 1;
                }

                if(($this->input->post('having_stock') == 'off') || ($this->input->post('having_stock') == false))
                {
                    $having_stock = 0;
                }
                else
                {
                    $having_stock = 1;
                }

                 if(($this->input->post('is_use_top') == 'off') || ($this->input->post('is_use_top') == false))
                {
                    $is_use_top = 0;
                }
                else
                {
                    $is_use_top = 1;
                }

                $data = array(
                       'user_group_name' => $user_group_name,
                       'user_group_caption' => $caption,
                       'territory_type_id' => $territory_type_id,
                       'parent_id' => $parent_id,
                       'istatus' => $istatus,
                       'having_stock' => $having_stock,
                       'is_use_top' => $is_use_top,
                       'updated_by' => $this->session->userdata('username')
                    );
                
                $this->db->where('user_group_id', $user_group_id);
                $query = $this->db->update('t_mtr_user_group', $data);

                if(isset($query))
                {
                    return true;
                } 
        }
        
        public function delete( $id ) {
                /*
                * Any non-digit character will be excluded after passing $id
                * from intval function. This is done for security reason.
                */
                
                $query = $this->db->delete('t_mtr_user_group', array( 'user_group_id' => $id ));
                if($query)
                {
                    return true;
                }
        } //end delete
    
    function get_spesific_group_data($update_group_id)
    {
        $this->db->select('user_group_id, user_group_name, user_group_caption, territory_type_id, istatus, parent_id, having_stock, is_use_top');
        $query = $this->db->get_where('t_mtr_user_group', array('user_group_id' => $update_group_id));
        return $query->result();
    }

    function get_territory_name()
    {
        $this->db->select('territory_type_id, territory_type_name');
        $this->db->order_by('territory_type_name');
        $sql = $this->db->get('t_mtr_territory_type');
        return $sql->result();
    }

    function get_parent()
    {
        $this->db->select('user_group_id, user_group_name');
        $this->db->order_by('user_group_name');
        $sql = $this->db->get('t_mtr_user_group');
        return $sql->result();
    }
} //end class